﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;

namespace EmailManager
{
    public class EmailManager
    {

        public class CustomAttachments
        {
            public string Filename;
            public System.IO.Stream FileStream;
        }


        public static void Send(string To, string Subject, string Body, bool IsBodyHtml)
        {
            SendManager(new List<string>() { To }, new List<string>(), Subject, Body, IsBodyHtml, new List<CustomAttachments>());
        }       
       
        public static void Send(List<string> To, List<string> CC, string Subject, string Body, bool IsBodyHtml)
        {
            SendManager(To, CC, Subject, Body, IsBodyHtml, new List<CustomAttachments>());
        }

        public static void Send(List<string> To, List<string> CC, string Subject, string Body, bool IsBodyHtml, List<CustomAttachments> Attachment)
        {
            SendManager(To, CC, Subject, Body, IsBodyHtml, Attachment);
        }

        private static void SendManager(List<string> To, List<string> CC, string Subject, string Body, bool IsBodyHtml, List<CustomAttachments> Attachment)
        {
            try
            {
                string From = ConfigurationManager.AppSettings["From"];
                string Host = ConfigurationManager.AppSettings["MailServer"];
                string UserName = ConfigurationManager.AppSettings["UserName"];
                string Password = ConfigurationManager.AppSettings["Password"];
                int Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
                bool EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
                string DeliveryMethod = ConfigurationManager.AppSettings["DeliveryMethod"];
                bool UseDefaultCredentials = Convert.ToBoolean(ConfigurationManager.AppSettings["UseDefaultCredentials"]);

                System.Net.Mail.MailMessage Mail = new System.Net.Mail.MailMessage();
                foreach (string toEntity in To)
                {
                    Mail.To.Add(toEntity);
                }

                foreach (string ccEntity in CC)
                {
                    Mail.CC.Add(ccEntity);
                }

                Mail.From = new System.Net.Mail.MailAddress(From);
                Mail.Subject = Subject;
                Mail.Body = Body;
                Mail.IsBodyHtml = IsBodyHtml;
                
                foreach (CustomAttachments streamtEntity in Attachment)
                {                                                             
                    Mail.Attachments.Add(new System.Net.Mail.Attachment(streamtEntity.FileStream, streamtEntity.Filename));                    
                }

                System.Net.Mail.SmtpClient Stmp = new System.Net.Mail.SmtpClient();
                Stmp.Host = Host;
                System.Net.NetworkCredential Credential = new System.Net.NetworkCredential();
                Credential.UserName = UserName;
                Credential.Password = Password;
                Stmp.UseDefaultCredentials = UseDefaultCredentials;
                Stmp.Credentials = Credential;
                Stmp.EnableSsl = EnableSsl;
                if (DeliveryMethod == System.Net.Mail.SmtpDeliveryMethod.Network.ToString())
                    Stmp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                Stmp.Port = Port;

                Stmp.Send(Mail);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                throw ex;
            }
        }

    }

}
